package com.cake.liulanxiangzhu.controller;

import com.cake.liulanxiangzhu.pojo.dto.AdminCategoryDTO;
import com.cake.liulanxiangzhu.pojo.dto.AdminCategoryInsertDTO;
import com.cake.liulanxiangzhu.pojo.vo.AdminCategoryVO;
import com.cake.liulanxiangzhu.service.ICategoryService;
import com.cake.liulanxiangzhu.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Validated
@RequestMapping("/category")
public class AdminCategoryController {
    @Autowired
    ICategoryService service;

    @GetMapping("/list")
    public JsonResult<List<AdminCategoryVO>> categoryList(){
        return JsonResult.ok(service.list());
    }

    @PostMapping("/addNew")
    public JsonResult<Void> addNew(@Valid AdminCategoryInsertDTO adminCategoryInsertDTO){
        service.addNew(adminCategoryInsertDTO);
        return JsonResult.ok();
    }

    @PostMapping("/deleteById")
    public JsonResult<Void> deleteById( Integer categoryId){
        service.delete(categoryId);
        return JsonResult.ok();
    }

    @PostMapping("/updateById")
    public JsonResult<Void> updateId(@Valid AdminCategoryDTO adminCategoryDTO){
        service.update(adminCategoryDTO);
        return JsonResult.ok();
    }


    @PostMapping("/selectById")
    public JsonResult<AdminCategoryVO> selectById( Integer categoryId){
        AdminCategoryVO vo = service.selectById(categoryId);
        return JsonResult.ok(vo);
    }

}
